/**
 * Copyright (c) 2010, QinJian
 * All rights reserved.
 */
package com.q.course.service.category;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.q.course.dao.category.CategoryDao;
import com.q.course.entity.category.Category;

/**
 * @author 覃健,Email:j411@qq.com
 *
 */
@Component
@Transactional
public class CategoryService {

	@Autowired
	private CategoryDao categoryDao;
	
	public Long saveCategory(Category category){
		
		categoryDao.save(category);
		return category.getId();
	}
	
	@Transactional( readOnly=true )
	public List<Category> getCategories(long type){
		return categoryDao.findByTypeAndExistState(type,true);
	}
	
	@Transactional( readOnly=true )
	public List<Category> getCategoriesParentIsNull(long type, Long categoryId){
		return categoryDao.findByTypeAndCategoryIdAndExistState(type, categoryId, true);
	}
	
	@Transactional( readOnly=true )
	public List<Category> getCategoriesByClass( Long classId ){
		Long cid = categoryDao.getCourseByClass(classId);
		return categoryDao.findByIdOrCategoryIdAndExistState(cid, cid, true);
	}
	
	public boolean deleteCategory(Category category){
		categoryDao.delete(category.getId());
		return true;
	}
}
